##################################
source $HOME/scriptbelt/bashutilities.sh
##################################
# 	THE OBJECTIVE IS TO CHANGE *ALL* *PARAMETERS* IN THIS FILE AND HAVE THE .DAT FILE CHANGES IN CONSEQUENCE
#########################################
#########################################
function label ()
#########################################
{
if [ "$1" -eq "0" ] && [ "$2" -eq "99" ] && [ "$3" -eq "0" ] && [ "$4" -eq "99" ] && [ "$5" -eq "0" ] && [ "$6" -eq "99" ] && [ "$7" == ".false." ]; then
    if  [ "$8" == "\'no\'" ]; then
      echo "inclusive"
    fi
    if [ "$8" == "\'HT\'" ]; then
      echo "scaleHT_inclusive"
    fi
    if [ "$8" == "\'sqrt\(M\^2+pt345\^2\)\'" ]; then
      echo "scaleMpT_inclusive"
    fi
fi
#
if (( $(bc <<< "$1 == 30") == 1 )) && (( $(bc <<< "$2 == 2.4") == 1 )) && (( $(bc <<< "$3 == 20") == 1 )) && (( $(bc <<< "$4 == 2.4") == 1 )) && (( $(bc <<< "$5 == 20") == 1 )) && (( $(bc <<< "$6 == 2.4") == 1 )) && [ "$7" == ".true." ];  then
    if  [ "$8" == "\'no\'" ]; then
      echo "cut1"
    fi
    if [ "$8" == "\'HT\'" ]; then
      echo "scaleHT_cut1"
    fi
    if [ "$8" == "\'sqrt\(M\^2+pt345\^2\)\'" ]; then
      echo "scaleMpT_cut1"
    fi
fi
#
if (( $(bc <<< "$1 == 30") == 1 )) && (( $(bc <<< "$2 == 2.4") == 1 )) && (( $(bc <<< "$3 == 0") == 1 )) && (( $(bc <<< "$4 == 99") == 1 )) && (( $(bc <<< "$5 == 0") == 1 )) && (( $(bc <<< "$6 == 99") == 1 )) && [ "$7" == ".false." ];  then
    if  [ "$8" == "\'no\'" ]; then
      echo "cut10"
    fi
    if [ "$8" == "\'HT\'" ]; then
      echo "scaleHT_cut10"
    fi
    if [ "$8" == "\'sqrt\(M\^2+pt345\^2\)\'" ]; then
      echo "scaleMpT_cut10"
    fi
fi
#
if (( $(bc <<< "$1 == 0") == 1 )) && (( $(bc <<< "$2 == 99") == 1 )) && (( $(bc <<< "$3 == 20") == 1 )) && (( $(bc <<< "$4 == 2.4") == 1 )) && (( $(bc <<< "$5 == 20") == 1 )) && (( $(bc <<< "$6 == 2.4") == 1 )) && [ "$7" == ".true." ];  then
    if  [ "$8" == "\'no\'" ]; then
      echo "cut01"
    fi
    if [ "$8" == "\'HT\'" ]; then
      echo "scaleHT_cut01"
    fi
    if [ "$8" == "\'sqrt\(M\^2+pt345\^2\)\'" ]; then
      echo "scaleMpT_cut01"
    fi
fi
#
if (( $(bc <<< "$1 == 25") == 1 )) && (( $(bc <<< "$2 == 2.4") == 1 )) && (( $(bc <<< "$3 == 20") == 1 )) && (( $(bc <<< "$4 == 2.4") == 1 )) && (( $(bc <<< "$5 == 20") == 1 )) && (( $(bc <<< "$6 == 2.4") == 1 )) && [ "$7" == ".true." ];  then
    if  [ "$8" == "\'no\'" ]; then
      echo "cut2"
    fi
    if [ "$8" == "\'HT\'" ]; then
      echo "scaleHT_cut2"
    fi
    if [ "$8" == "\'sqrt\(M\^2+pt345\^2\)\'" ]; then
      echo "scaleMpT_cut2"
    fi
fi
#
if (( $(bc <<< "$1 == 35") == 1 )) && (( $(bc <<< "$2 == 2.4") == 1 )) && (( $(bc <<< "$3 == 20") == 1 )) && (( $(bc <<< "$4 == 2.4") == 1 )) && (( $(bc <<< "$5 == 20") == 1 )) && (( $(bc <<< "$6 == 2.4") == 1 )) && [ "$7" == ".true." ];  then
    if  [ "$8" == "\'no\'" ]; then
      echo "cut3"
    fi
    if [ "$8" == "\'HT\'" ]; then
      echo "scaleHT_cut3"
    fi
    if [ "$8" == "\'sqrt\(M\^2+pt345\^2\)\'" ]; then
      echo "scaleMpT_cut3"
    fi
fi
#
if (( $(bc <<< "$1 == 30") == 1 )) && (( $(bc <<< "$2 == 1.5") == 1 )) && (( $(bc <<< "$3 == 20") == 1 )) && (( $(bc <<< "$4 == 2.4") == 1 )) && (( $(bc <<< "$5 == 20") == 1 )) && (( $(bc <<< "$6 == 2.4") == 1 )) && [ "$7" == ".true." ];  then
    if  [ "$8" == "\'no\'" ]; then
      echo "cut4"
    fi
    if [ "$8" == "\'HT\'" ]; then
      echo "scaleHT_cut4"
    fi
    if [ "$8" == "\'sqrt\(M\^2+pt345\^2\)\'" ]; then
      echo "scaleMpT_cut4"
    fi
fi
#
}
#########################################
function run_mcfm () 
#########################################
{
##### MACHINE DEPENDNEDT PARAMTERS ######
# FETCHED IN THE LOCALCONFIGFILE: mcfm_mtop_mu_scan.conf
source mcfm_mtop_mu_scan.conf
#queue=2nw
#cluster=condor
#jobstoqueue=24
#points=2000000
#pointssmall=200000
mass=$1
muren=$2
mufact=$3
radio=$4 
raggio=`echo $( echo "scale=2; $radio / 10" | bc -l ) | sed -e 's/^\./0./' -e 's/^-\./-0./'`
#########################################################
################# CUTS ##################################
#########################################################
pt="$5"
eta="$6"
ptl="$7"
etal="$8"
ptltwo="$9"
etaltwo="${10}"
if (( $(bc <<< "$ptl == 0") == 1 )) && (( $(bc <<< "$ptltwo == 0") == 1 )) && (( $(bc <<< "$etal == 99") == 1 )) && (( $(bc <<< "$etaltwo == 99") == 1 ));  then
    docuts=".false."
else
    docuts=".true."
fi
scale="${11}"
#scale="\'no\'"
#scale="\'HT\'"
#scale="\'sqrt\(M\^2+pt345\^2\)\'"
if [ "$scale" == "\'no\'" ]; then
   muren=$(echo  "$mass"*$2 | bc -l)
   mufact=$(echo  "$mass"*$3 | bc -l)
fi
#########################################
#    PDFs    ############################
#########################################
#
pdf="${12}"
#
lhc="${13}"
clusteringalg="${14}"
cuts[0]=$pt
cuts[1]=$eta
cuts[2]=$ptl
cuts[3]=$etal
cuts[4]=$ptltwo
cuts[5]=$etaltwo
cuts[6]="$docuts"
cuts[7]="$scale"
cutlabel=`label ${cuts[*]}`	
#########################################################
nabc=`echo $( echo "scale=1; $points / 1000000" | bc )"M"`
#########################################################
#***  PARAMETERS fixed in each folder wil not be changed here   ****
#########################################################
#set_mcfm_parameter.sh nproc 141 --file=$basefile -v
#set_mcfm_parameter.sh order \'todk\' --file=$basefile -v
#set_mcfm_parameter.sh inclusive .true. --file=$basefile -v
#set_mcfm_parameter.sh zerowidth .false. --file=$basefile -v
#########################################################
#
#########################################################
### 	RUN SPECIFIC
#########################################################
#
set_mcfm_parameter.sh Rcut_jet "$raggio"d0 --file=$basefile -v
set_mcfm_parameter.sh s "$lhc"000d0 --file=$basefile -v
set_mcfm_parameter.sh ncall1 $pointssmall --file=$basefile -v
set_mcfm_parameter.sh ncall2 $points --file=$basefile -v
set_mcfm_parameter.sh makecuts "$docuts" --file=$basefile -v
set_mcfm_parameter.sh ptjet_min "$pt"d0 --file=$basefile -v
set_mcfm_parameter.sh ptlepton_min "$ptl"d0 --file=$basefile -v
set_mcfm_parameter.sh ptlepton\(2nd+\)_min "$ptltwo"d0 --file=$basefile -v
set_mcfm_parameter.sh \|etajet\|_max "$eta"d0 --file=$basefile -v
set_mcfm_parameter.sh \|etalepton\|_max "$etal"d0 --file=$basefile -v
set_mcfm_parameter.sh \|etalepton\(2nd+\)\|_max "$etaltwo"d0 --file=$basefile -v
set_mcfm_parameter.sh mt "$mass"d0 --file=$basefile -v
set_mcfm_parameter.sh muR "$muren"d0 --file=$basefile -v
set_mcfm_parameter.sh muF "$mufact"d0 --file=$basefile -v
set_mcfm_parameter.sh dynamicscale "$scale" --file=$basefile -v
set_mcfm_parameter.sh pdlabel "$pdf" --file=$basefile -v
set_mcfm_parameter.sh algorithm "$clusteringalg" --file=$basefile -v
#set_mcfm_parameter.sh ij 2173 --file=$basefile -v
#
# ******************************************************
# ******************************************************
# ******************************************************
#runname=LHC"$lhc"_"$nabc"_mt"$mass"_pTj"$pt"_r"$radio"_eta"$eta"_inclusive 
runname=LHC"$lhc"_"$nabc"_mt"$mass"_pTj"$pt"_r"$radio"_eta"$eta"_"$cutlabel" 
set_mcfm_parameter.sh name \'$runname\' --file=$basefile -v
# ******************************************************
# ******************************************************
# ******************************************************
#################
    echo -e "will run with"$RED" mtop="$mass$COL_RESET
    echo -e "will run with"$RED" μ(F)="$mufact$COL_RESET
    echo -e "will run with"$RED" μ(R)="$muren$COL_RESET
timestamp=`date +%F_%H-%M-%S__%s`
uniquefile=$basefile.$mass.$mufact.$muren"_R"$raggio"_"$timestamp".DAT"
cp $basefile $uniquefile

if [ $cluster = condor ]; then
    #make condor_mcfm.jdl
    cat condor_mcfm.jdl.base > condor_mcfm.jdl
    args='Arguments = $(Cluster) $(Process)'" ""$uniquefile"" "$jobstoqueue
    echo "$args" >> condor_mcfm.jdl
    echo "transfer_input_files = process.DAT,mcfm,PDFs.tar.gz,$uniquefile" >> condor_mcfm.jdl
    echo "Queue $jobstoqueue" >> condor_mcfm.jdl
    # pass argument $uniquefile to run './mcfm $uniquefile'
    condor_submit condor_mcfm.jdl
    #./mcfm
fi
#
#if [ $cluster = lsf ]; then
#   thisclusterID=332
#   for i in $(seq 1 $jobstoqueue); do 
        #bsub -q $queue -J "$uniquefile""[1-"$jobstoqueue"]" "batch.q `pwd` $uniquefile $jobstoqueue"
	#echo "about to submit LSF from directory "`pwd -L`
#        bsub -q $queue -e myjob.err -o myjob.out  -J "$uniquefile""[1-"$jobstoqueue"]" "`pwd -L`/batch.q `pwd` $uniquefile $jobstoqueue"
#   done
#fi

if [ $cluster = lsf ]; then
        echo "running with ID: "$lastID
        bsub -u dsfjkskjfs -q $queue -J "$uniquefile""[1-"$jobstoqueue"]" "`pwd -L`/batch.q `pwd` $uniquefile $jobstoqueue $lastID"
        let "lastID += 1"  
        echo "stored variable ID: "$lastID
fi

if [ $cluster = local ]; then
    read -p "Are you sure? " -n 1 -r
    echo    # (optional) move to a new line
if [[ $REPLY =~ ^[Yy]$  ]]
then
#sleep 60;
 ./mcfm $uniquefile
fi
fi

#################
#mutt -s "MCFM 173 inclusive" rfrances@umd.edu < input.DAT
#cp -v *$runname* ../../results/
#########################################################
}

#########################################################
################## RUN DEFAULTS #########################
#########################################################
basefile=input.DAT.base
knownthisrun=false
#offdiagrun=true
offdiagrun=false
# get lastID from $HOME
lastID=`more ~/lastID`
#########################################################
############### PHYSICS DEFAULT #########################
#########################################################
aktradius=7
ptjCLI=0
etajCLI=99
ptlCLI=0
etalCLI=99
ptltwoCLI=0
etaltwoCLI=99
scaleCLI="\'no\'"
pdfCLI="\'mstw8nl\'"
clusteringCLI="\'ankt\'"
sqrtsCLI=14
#########################################################
#########################################################
#########################################################
for iarg in $*; do
    CLIinputs[$argCounter]=$iarg
    option=`echo "$iarg" | cut -d"=" -f1`
    
    if [ "$option" == "-h" ]; then
        echo -e "available options are:"
        echo -e $RED"--inputifle"$COL_RESET"(default:""$basefile"")"
        echo -e $RED"--radius"$COL_RESET" (default:"$aktradius")"
        echo -e $RED"--offdiagrun"$COL_RESET" true,false (default:"$offdiagrun")"
        echo -e $RED"--ptj"$COL_RESET": default="$ptjCLI
        echo -e $RED"--etaj"$COL_RESET": default="$etajCLI
        echo -e $RED"--ptl"$COL_RESET": default="$ptlCLI
        echo -e $RED"--etal"$COL_RESET": default="$etalCLI
        echo -e $RED"--ptl2"$COL_RESET": default=ptl1"
        echo -e $RED"--etal2"$COL_RESET": default=etal1 "
        echo -e $RED"--scale"$COL_RESET": fixed -> no, HT, MpT (default="$scaleCLI")"
        echo -e $RED"--pdf"$COL_RESET": CT10 -> CT10.00, MSTW08 -> mstw8nl (default="$pdfCLI")"
        echo -e $RED"--clustering"$COL_RESET": AKT -> ankt, KT -> ktal (default="$clusteringfCLI")"
        echo -e $RED"--sqrts"$COL_RESET": default="$sqrtsCLI
    fi

    if [ "$option" == "--inputfile" ]; then
	basefile=`echo "$iarg" | cut -d"=" -f2`
    	echo -e "will run editing the file "$RED$basefile$COL_RESET
    fi

    if [ "$option" == "--radius" ]; then
        aktradius=`echo "$iarg" | cut -d"=" -f2`
        echo -e "will make jets with R="$RED$aktradius$COL_RESET
    fi

    if [ "$option" == "--ptj" ]; then
        ptjCLI=`echo "$iarg" | cut -d"=" -f2`
        echo -e "will make jets with pt>"$RED$ptjCLI$COL_RESET
    fi

    if [ "$option" == "--etaj" ]; then
        etajCLI=`echo "$iarg" | cut -d"=" -f2`
        echo -e "will make jets with eta<"$RED$etajCLI$COL_RESET
    fi

    if [ "$option" == "--ptl" ]; then
        ptlCLI=`echo "$iarg" | cut -d"=" -f2`
        ptltwoCLI=`echo "$iarg" | cut -d"=" -f2`
        echo -e "will require both leptons with ptl>"$RED$ptlCLI$COL_RESET
    fi

    if [ "$option" == "--etal" ]; then
        etalCLI=`echo "$iarg" | cut -d"=" -f2`
        etaltwoCLI=`echo "$iarg" | cut -d"=" -f2`
        echo -e "will require both leptons with eta<"$RED$etalCLI$COL_RESET
    fi

    if [ "$option" == "--ptltwo" ]; then
        ptltwoCLI=`echo "$iarg" | cut -d"=" -f2`
        echo -e "will require the 2nd lepton with ptl>"$RED$ptltwoCLI$COL_RESET
    fi

    if [ "$option" == "--etaltwo" ]; then
        etaltwoCLI=`echo "$iarg" | cut -d"=" -f2`
        echo -e "will require the 2nd lepton with eta<"$RED$etaltwoCLI$COL_RESET
    fi

    if [ "$option" == "--pdf" ]; then
        thisPDF=`echo "$iarg" | cut -d"=" -f2`
        case $thisPDF in
            CT10 )
                pdfCLI="\'CT10.00\'"
                echo -e "will use parton distribution functions "$RED$pdfCLI$COL_RESET
                ;;
            MSTW08 )
                pdfCLI="\'mstw8nl\'"
                echo -e "will use parton distribution functions "$RED$pdfCLI$COL_RESET
                ;;
        esac
    fi

    if [ "$option" == "--sqrts" ]; then
        sqrtsCLI=`echo "$iarg" | cut -d"=" -f2`
        echo -e "will collide at Sqrt[S]="$RED$sqrtsCLI$COL_RESET" TeV"
    fi

    if [ "$option" == "--clustering" ]; then
        thisclustering=`echo "$iarg" | cut -d"=" -f2`
        case $thisclustering in
            AKT )
                clusteringCLI="\'ankt\'"
                echo -e "will cluster partons with algorithm "$RED$clusteringCLI$COL_RESET" TeV"
                ;;
            KT )
                clusteringCLI="\'ktal\'"
                echo -e "will cluster partons with algorithm "$RED$clusteringCLI$COL_RESET" TeV"
                ;;
        esac
    fi

    if [ "$option" == "--offdiagrun" ]; then
        offdiagrun=`echo "$iarg" | cut -d"=" -f2`
        echo -e "will run all scales: "$RED$offdiagrun$COL_RESET
    fi

    if [ "$option" == "--scale" ]; then
        thisscale=`echo "$iarg" | cut -d"=" -f2`
        echo -e "will use scale="$RED$thisscale$COL_RESET
        case $thisscale in
            fixed )
                scaleCLI="\'no\'"
                echo -e "the scale will be "$RED$thisscale$COL_RESET
                ;;
            HT )
                scaleCLI="\'HT\'"
                echo -e "the scale will be "$RED$thisscale$COL_RESET
                ;;
            MpT )
		scaleCLI="\'sqrt\(M\^2+pt345\^2\)\'"
                echo -e "the scale will be "$RED$thisscale$COL_RESET
                ;;
        esac
    fi

#    if [ "$option" == "--cluster" ]; then
#        cluster=`echo "$iarg" | cut -d"=" -f2`
#        echo -e "will submit jobs to "$RED$cluster$COL_RESET
#    fi

    if [ "$option" == "--single" ]; then
	
	thisrun=`echo "$iarg" | cut -d"=" -f2`
	case $thisrun in
	    low )
		knownthisrun=true
		echo -e "the only run will be "$RED$thisrun$COL_RESET
		;;
	    high )
		knownthisrun=true
		echo -e "the only run will be "$RED$thisrun$COL_RESET
		;;
	    central )
		knownthisrun=true
		echo -e "the only run will be "$RED$thisrun$COL_RESET
		;;
	esac
    fi
done

for iarg in $*; do
    if [[ ${iarg:0:1} != "-" ]] ; then
	mtop=$iarg
	mufhigh=2 #$(echo  "$mtop"*2 | bc -l)
	murhigh=2 #$(echo  "$mtop"*2 | bc -l)
	mufflow=0.5 #$(echo  "$mtop"*0.5 | bc -l)
	murlow=0.5 #$(echo  "$mtop"*0.5 | bc -l)
	mufcentral=1
        murcentral=1
#
physicsparams[0]=$aktradius
physicsparams[1]=$ptjCLI
physicsparams[2]=$etajCLI
physicsparams[3]=$ptlCLI
physicsparams[4]=$etalCLI
physicsparams[5]=$ptltwoCLI
physicsparams[6]=$etaltwoCLI
#physicsparams[7]="$docuts"
physicsparams[7]="$scaleCLI"
physicsparams[8]="$pdfCLI"
physicsparams[9]=$sqrtsCLI
physicsparams[10]="$clusteringCLI"
#physicsparams[9]=$anewparam
#
#
# syntax: run_mcfm $mtop $mufflow $murlow ${physicsparams[*]} 
#
#
	if [ $knownthisrun = true ]; then
	    case $thisrun in
		low )
		    #run_mcfm $mtop $mufflow $murlow $aktradius
		    run_mcfm $mtop $mufflow $murlow ${physicsparams[*]} 
		    ;;
		high )
		    #run_mcfm $mtop $mufhigh $murhigh $aktradius
		    run_mcfm $mtop $mufhigh $murhigh ${physicsparams[*]}
		    ;;
		central )
		    #run_mcfm $mtop $mufcentral $murcentral $aktradius
		    run_mcfm $mtop $mufcentral $murcentral ${physicsparams[*]} 
		    ;;
	    esac	

	else
	    #run_mcfm $mtop $mufcentral $murcentral $aktradius
	    run_mcfm $mtop $mufcentral $murcentral ${physicsparams[*]} 
		sleep 60
	    #run_mcfm $mtop $mufhigh $murhigh $aktradius
	    run_mcfm $mtop $mufhigh $murhigh ${physicsparams[*]} 
		sleep 60
	    #run_mcfm $mtop $mufflow $murlow $aktradius
	    run_mcfm $mtop $mufflow $murlow ${physicsparams[*]}
		sleep 60

          if [ $offdiagrun = true ]; then
            #run_mcfm $mtop $mufcentral $murhigh $aktradius
            run_mcfm $mtop $mufcentral $murhigh ${physicsparams[*]}
                sleep 60
            #run_mcfm $mtop $mufcentral $murlow $aktradius
            run_mcfm $mtop $mufcentral $murlow ${physicsparams[*]}
                sleep 60
            #run_mcfm $mtop $mufhigh $murcentral $aktradius
            run_mcfm $mtop $mufhigh $murcentral ${physicsparams[*]}
                sleep 60
            #run_mcfm $mtop $mufflow $murcentral $aktradius
            run_mcfm $mtop $mufflow $murcentral ${physicsparams[*]}
                sleep 60
            #run_mcfm $mtop $murlow $murhigh $aktradius
            run_mcfm $mtop $murlow $murhigh ${physicsparams[*]}
                sleep 60
            #run_mcfm $mtop $murhigh $murlow $aktradius
            run_mcfm $mtop $murhigh $murlow ${physicsparams[*]}
                sleep 60
          fi
	fi
    fi

echo $lastID > ~/lastID
done
